#Author: Mike Schladt - 2014
#Util to create and store tags from json backups to mysql schema  

import os
import sys
import json
import lib.mipreport as mipreport
from lib.mipsample import MipSample

json_arch_dir = '/home/mike/share/json_arch/'

#find all files in json archive (recurses directories)
for root, dirs, files in os.walk(json_arch_dir):
    for file in files:
        print "Processing {0}".format(file)
        
        try:        
            file_path = os.path.join(root,file)
            data = open(file_path)
            report = json.load(data)
        except Exception as e:
            print "Error loading file: {0}".format(e)
            raise 

        #create blank sample object
        sample = MipSample()
        #fill in details form json file
        sample.sha1 = report['target']['file']['sha1']
        sample.sha256 = report['target']['file']['sha256']
        sample.sha512 = report['target']['file']['sha512']
        sample.md5 = report['target']['file']['md5']
        sample.ssdeep = report['target']['file']['ssdeep']
        sample.name = report['target']['file']['name']  
        sample.size = report['target']['file']['size']
        sample.filetype = report['target']['file']['type']
      
        #store sample attribute to database
        try :        
            sample.store_db()

        except Exception as e:
            print "Error storing sample data: {0}".format(e)

        #parse report to create tags
        try : 
            tags = mipreport.create_tags(report)
        except Exception as e:
            print "Error parsing JSON file: {0}".format(e) 
        
        #store tags to MySQL db
        try :  
            mipreport.store_tags(sample.sid,tags)
        except Exception as e:
            print "Error storing tags: {0}".format(e)


